package com.bidevalution.business.vote.service.impl;

import com.bidevalution.business.exception.VoteException;
import com.bidevalution.business.service.BeSectionColumnService;
//import com.bidevalution.business.vote.service.AutoVoteBaseService;
import com.bidevalution.business.vote.launch.AutoVoteBaseLaunch;
import com.bidevalution.business.vote.service.BeAutoVoteConfigService;
import com.bidevalution.business.vote.vi.AutoVoteJobVi;
import com.bidevalution.dao.entity.BeAutoVoteConfig;
import com.bidevalution.dao.entity.BeSectionColumn;
import com.bidevalution.dao.enums.VoteLocationEnum;
import com.bidevalution.dao.mapper.BeAutoVoteConfigMapper;
import com.bidevalution.support.context.ConstantsByVoteEnum;
import com.bidevalution.support.context.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BeAutoVoteConfigServiceImpl implements BeAutoVoteConfigService {
    @Autowired
    private BeAutoVoteConfigMapper beAutoVoteConfigMapper;

    @Autowired
    private BeSectionColumnService beSectionColumnService;


    @Override
    public List<BeAutoVoteConfig> findBeAutoVotesByVoteLocation(VoteLocationEnum voteLocationEnum) {
        return beAutoVoteConfigMapper.findBeAutoVotesByVoteLocation(voteLocationEnum.name());
    }

    @Override
    public void launchAutoVote(VoteLocationEnum voteLocationEnum, AutoVoteJobVi autoVoteJobVi) {
        BeSectionColumn ignore = beSectionColumnService.getBeSectionColumn(autoVoteJobVi.getSectionStepId(), ConstantsByVoteEnum.ignoreVote);
        if (ignore != null) {
            return;
        }
        List<BeAutoVoteConfig> list = findBeAutoVotesByVoteLocation(voteLocationEnum);
        try {
            for (BeAutoVoteConfig item: list) {
                AutoVoteBaseLaunch baseAutoVoteService = (AutoVoteBaseLaunch) SpringContextUtil.getBean(item.getVoteBeanName());
                baseAutoVoteService.execute(autoVoteJobVi);
            }
        } catch (VoteException e) {
            e.printStackTrace();
        }
    }
}
